package com.uestc.servlet.ui;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uestc.admin.AdminBase;
import com.uestc.admin.AdminException;
import com.uestc.admin.AdminFactory;
import com.uestc.servlet.ServletBase;
import com.uestc.util.Log;

/**
 * 返回某个选项对应的信息，比如用户管理，文件系统管理
 * 
 * @author justyoung
 *
 */
@WebServlet("/admin/TreeAdmin")
public class TreeAdmin extends HttpServlet implements ServletBase {
	private static final long serialVersionUID = 1L;

	public TreeAdmin() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		AdminBase ab = null;
		String opt = getParameter(request, "treeOpt");
		System.out.println(opt);
		if (opt.contains("sub")) {
			opt = opt.substring(0, opt.length() - 3);
			String startPage = request.getParameter("start");
			String limit = request.getParameter("limit");
			try {
				ab = AdminFactory.getInstance(opt);
				writeMessage(response, ab.getInfo(Integer.valueOf(startPage), Integer.valueOf(limit), request));
				System.out.println(startPage + " " + limit);
			} catch (Throwable e) {
				Log.logException(e);
			}
		} else {
			try {
				ab = AdminFactory.getInstance(opt);
				writeMessage(response, ab.getColumns());
			} catch (AdminException e) {
				Log.logException(e);
			}
		}
	}
}
